package com.xt.stock.project.service.data;

import com.xt.stock.project.common.constant.PlateConstant;
import com.xt.stock.project.common.constant.TushareConstant;
import com.xt.stock.project.common.constant.XtBasicConstant;
import com.xt.stock.project.common.constant.XtCodeConstant;
import com.xt.stock.project.common.exception.XtServiceException;
import com.xt.stock.project.dao.basic.StockMapper;
import com.xt.stock.project.model.stock.Stock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.*;

@Service
public class TushareService {

    @Autowired
    private StockMapper stockMapper;

    /**
     * 获取股票基本信息
     */
    public void getStockData() {
        List<Stock> stockList = new ArrayList<>();
        List<List<String>> list = null;

        //抽取数据
        Map<String,String> map = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("api_name",TushareConstant.STOCK_BASIC);
        params.put("token", TushareConstant.TUSHARE_TOKEN);
        params.put("params",map);
        try {
            RestTemplate restTemplate = new RestTemplate();
            //执行HTTP请求，将返回的结构使用ResultVO类格式化
            Map<String,Object> result = restTemplate.postForObject(TushareConstant.TUSHARE_URL, params, Map.class);
            Map<String,String> stockMap = (Map<String, String>) result.get("data");
            List<Object> objectList = Collections.singletonList(stockMap.get("items"));
            list = (List<List<String>>) objectList.get(0);
        } catch (Exception e) {
            throw new XtServiceException(XtCodeConstant.ERROR_DATA_GET,"股票基础数据获取失败!");
        }

        //将数据存入数据库
        transformStock(stockList,list);
        stockMapper.insert(stockList.get(1));
    }

    private void transformStock(List<Stock> stockList,List<List<String>> list) {
        Stock stock;
        for (List<String> vo : list) {
            stock = new Stock();
            stock.setName(vo.get(2));
            stock.setCode(vo.get(1));
            stock.setPlate(vo.get(0).substring(vo.get(0).length()-2));
            if(PlateConstant.MAIN_BOARD_MARKE.equals(vo.get(5))) {
                stock.setPlateCode(XtBasicConstant.BYTE_ONE);
            } else if(PlateConstant.MIDDLE_BOARD_MARKE.equals(vo.get(5))) {
                stock.setPlateCode(XtBasicConstant.BYTE_TWO);
            } else if(PlateConstant.SECOND_BOARD_MARKE.equals(vo.get(5))) {
                stock.setPlateCode(XtBasicConstant.BYTE_THREE);
            } else if(PlateConstant.STAR_MARKET.equals(vo.get(5))) {
                stock.setPlateCode(XtBasicConstant.BYTE_FOUR);
            }
            stock.setListTime(vo.get(6));
            stock.setEnableFlag(XtBasicConstant.BYTE_ONE);
            stock.setDeleteFlag(XtBasicConstant.BYTE_ZERO);
            stock.setCreateTime(System.currentTimeMillis());
            stock.setCreateUid(0);

            stockList.add(stock);
        }
    }

    /**
     * 日线行情数据获取
     */
    public void getDailyData() {
        List<List<String>> list = null;

        //抽取数据
        Map<String,String> map = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("api_name",TushareConstant.DAILY);
        params.put("token", TushareConstant.TUSHARE_TOKEN);
        params.put("params",map);

        RestTemplate restTemplate = new RestTemplate();
        try {
            //执行HTTP请求，将返回的结构使用ResultVO类格式化
            Map<String,Object> result = restTemplate.postForObject(TushareConstant.TUSHARE_URL, params, Map.class);
            Map<String,String> stockMap = (Map<String, String>) result.get("data");
            List<Object> objectList = Collections.singletonList(stockMap.get("items"));
            list = (List<List<String>>) objectList.get(0);
        } catch (Exception e) {
            throw new XtServiceException(XtCodeConstant.ERROR_DATA_GET,"股票基础数据获取失败!");
        }

    }

}
